import requests
from fontTools.ttLib import TTFont
from PIL import Image, ImageFont, ImageDraw
from ddddocr import DdddOcr
import io
import re


url = 'https://piaofang.maoyan.com/dashboard-ajax/movie?orderType=0&uuid=19a54257977c8-0f8f7b7d23369f-26061b51-1fa400-19a54257978c8&timeStamp=1763457652417&User-Agent=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzE0Mi4wLjAuMCBTYWZhcmkvNTM3LjM2&index=340&channelId=40009&sVersion=2&signKey=e26b377eb306df7b9c31cca1fae922e9&WuKongReady=h5'
headers = {
    "GET": "/dashboard-ajax?orderType=0&uuid=19a54257977c8-0f8f7b7d23369f-26061b51-1fa400-19a54257978c8&timeStamp=1763457192531&User-Agent=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzE0Mi4wLjAuMCBTYWZhcmkvNTM3LjM2&index=724&channelId=40009&sVersion=2&signKey=4e9a1e625952155bb7f8dbbddcfccaab&WuKongReady=h5 HTTP/1.1",
    "Cookie": "_lxsdk_cuid=19a54257977c8-0f8f7b7d23369f-26061b51-1fa400-19a54257978c8; _lxsdk=C4266410C43011F09BD1BBABBFCD7F8E381035E0B2204225B0F59579D7599D48; _ga=GA1.1.1942994162.1763437420; _ga_WN80P4PSY7=GS2.1.s1763437420$o1$g1$t1763437640$j29$l0$h0; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; _lxsdk_s=19a96234e35-73a-fa9-728%7C%7C5",
    "Referer": "https://piaofang.maoyan.com/dashboard?movieId=554092",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
}


res = requests.get(url=url, headers=headers)

def make_font_maps(path):
    ocr = DdddOcr(show_ad=False)
    font = TTFont(path)
    img_font = ImageFont.truetype(path, size=24)
    font_maps = {}
    for code,name in font.getBestCmap().items():
        if code == 120:
            continue
        # print(code,chr(code),name)

        img = Image.new('RGB',(35,35),(255,255,255))

        draw = ImageDraw.Draw(img, 'RGB')

        draw.text((0,0),chr(code),(0,0,0),font=img_font)

        # img.save(f'./static/{code}.png','png')
        img_bytes = io.BytesIO()
        img.save(img_bytes, 'png')
        img_value = img_bytes.getvalue()


        r = ocr.classification(img_value)
        font_maps[code] = r
    return font_maps



r = re.search(r'\),url\("(.*?)"\);}', res.json()['fontStyle'])
if r:
    font_url = f"https:{r.group(1)}"
    font_res = requests.get(font_url)

    with open('./static/font/my_font', "wb") as f:
        f.write(font_res.content)

    # 字符映射表
    font_maps = make_font_maps('./static/font/my_font')


def parse_field(value):

    item = []
    data = value.split(';')[:-1]

    for v in data:
        if v.startswith('.'):
            item.append('.')
            item.append(v[1:])
        else:
            item.append(v)
    result = ''
    for v2 in item:
        if v2 == '.':
            result += '.'
        else:
            code = int(v2[3:], base=16)
            if code in font_maps:
                data2 = font_maps[code]
                result += data2
    result += '万'
    return result


movies = res.json()['movieList']['list']
for movie in movies:
    print(movie['movieInfo']['movieName'],parse_field(movie['boxSplitUnit']['num']))
    # print(movie['splitBoxSplitUnit']['num'])


print(parse_field(res.json()['movieList']['nationBoxInfo']['nationBoxSplitUnit']['num']))
